package com.wang.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.apache.hadoop.io.IOUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.URI;


public class HdfsClient {

	private FileSystem fs;

	@Before
	public void before() throws IOException, InterruptedException {
		fs = FileSystem.get(URI.create("hdfs://10.0.0.114:9000"), new Configuration(), "root");
		System.out.println("<======【before】======>");
	}

	@After
	public void after() throws IOException {
		fs.close();
		System.out.println("<======【after】======>");
	}

	/**
     * 上传文件
	 * @throws IOException
	 */
	@Test
	public void put() throws IOException {
		// 用这个对象操作文件系统
		fs.copyFromLocalFile(new Path("D:\\input"), new Path("/"));
	}

	/**
	 * 下载文件
	 * @throws IOException
	 */
	@Test
	public void get() throws IOException {
		fs.copyToLocalFile(new Path("/WcTest.txt"), new Path("D:\\"));
	}

	/**
	 * 重命名文件
	 * @throws IOException
	 */
	@Test
	public void rename() throws IOException {
		fs.rename(new Path("/WcTest.txt"), new Path("/test2.txt"));
	}

	/**
	 * 删除文件
	 * @throws IOException
	 */
	@Test
	public void delete() throws IOException {
		boolean delete = fs.delete(new Path("/test2.txt"), true);
		if (delete) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
	}

	/**
	 * 将本地文件追加到服务器文件中
	 * @throws IOException
	 */
	@Test
	public void appendFile() throws IOException {
		FSDataOutputStream append = fs.append(new Path("/WcTest.txt"), 1024);
		FileInputStream open = new FileInputStream("D:\\1.txt");
		IOUtils.copyBytes(open, append, 1024, true);
	}

	/**
	 * 查看所有文件和文件夹
	 * @throws IOException
	 */
	@Test
	public void ls() throws IOException {
		FileStatus[] fileStatuses = fs.listStatus(new Path("/"));

		for (FileStatus fileStatus : fileStatuses) {
			if (fileStatus.isFile()) {
				System.out.println("以下是一个文件的信息");
				System.out.println(fileStatus.getPath());
				System.out.println(fileStatus.getLen());
			} else {
				System.out.println("这是一个文件夹");
				System.out.println(fileStatus.getPath());
			}
		}
	}

	/**
	 * 查看所有文件
	 * @throws IOException
	 */
	@Test
	public void listFiles() throws IOException {
		RemoteIterator<LocatedFileStatus> files = fs.listFiles(new Path("/"), true);

		while (files.hasNext()) {
			LocatedFileStatus file = files.next();

			System.out.println("======================================");
			System.out.println(file.getPath());

			System.out.println("块信息：");
			BlockLocation[] blockLocations = file.getBlockLocations();
			for (BlockLocation blockLocation : blockLocations) {
				String[] hosts = blockLocation.getHosts();
				for (String host : hosts) {
					System.out.println(host);
				}
			}
		}
	}



}
